<?php
/**
 * 用户肤质管理 微信H5 页面获取肤质检测记录接口
 *
 * Author: Zeon
 * Date: 2018/4/2
 * Created by Panxsoft.
 */
namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\ApiController;
use App\Http\Middleware\WechatAuth;
use App\Http\Requests\Wechat\DeprecatedSkinRecordIndexRequest;
use App\Models\Panx\SkinDetector;
use App\Models\Store\CamSkinRecord;
use App\Models\Store\SkinRecord;
use App\Models\Store\Store;
use App\Services\Presenters\HmSkinData;
use App\Services\Presenters\SkinRecordPresenter;
use App\Services\Presenters\WaxSkinData;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;

class SkinRecordController extends ApiController
{
    const ROUTE = [
        'show' => 'api.wechat.skin.record.show'
    ];

    /**
     * @deprecated (废弃了！)
     * @see \App\Http\Controllers\Wechat\V2_3\SkinRecordController::index()
     *
     * @param DeprecatedSkinRecordIndexRequest $request
     *
     * @return \Illuminate\Http\Response
     */
    public function index(DeprecatedSkinRecordIndexRequest $request)
    {
        $wechat_user = WechatAuth::$wechat_user;

        $skin_records = (new SkinRecord)
            ->where('wechat_user_id', $wechat_user->id)
            ->when($request->customer_id, function (Builder $builder, $customer_id) {
                $builder->whereIn('store_id', $this->getStoreIdsByCustId($customer_id));
            })
            ->when($request->store_id, function (Builder $builder, $store_id) {
                $builder->where('store_id', $store_id);
            })
            ->with('store', 'waxSkinRecords', 'camSkinRecords', 'hmSkinRecords')
            ->latest()
            ->get();

        $skin_records = $this->assembleSkinRecord($skin_records);

        return $this->response(compact('skin_records'));
    }

    /**
     * 用户测肤数详情
     * @test
     *
     * @param $skin_record_id
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function show($skin_record_id)
    {
        /** @var SkinRecord $skin_record */
        $skin_record = SkinRecord::find($skin_record_id);
            $this->checkItemExist($skin_record, trans('wechat.skin_record'));

        $customer_id = WechatAuth::$customer_id;

        switch($skin_record->skin_detector_id)
        {
            case SkinDetector::ID_CAMERA:
                /** @var CamSkinRecord $cam_skin_record */
                $cam_skin_record = $skin_record->camSkinRecords()->first();
                $skin_record = SkinRecordPresenter::assembleDetailCamSkinRecord($cam_skin_record);
                break;
            case SkinDetector::ID_WAX:
                $wax_skin_records = $skin_record->waxSkinRecords;
                $wax_suggests = WaxSkinData::getWaxSkinSuggest($customer_id);
                $skin_record = SkinRecordpresenter::assembleDetailWaxSkinRecords($wax_skin_records, $wax_suggests);
                break;
            case SkinDetector::ID_HM:
                $hm_skin_records = $skin_record->hmSkinRecords;
                $hm_suggests = HmSkinData::getHmSkinSuggest($customer_id);
                $skin_record = SkinRecordpresenter::assembleDetailHmSkinRecords($hm_skin_records, $hm_suggests);
                break;
            default:
                $skin_record = [];
        }

        return $this->response(compact('skin_record'));
    }

    /**
     * @param Collection $skin_records
     * @return Collection
     */
    private function assembleSkinRecord(Collection $skin_records)
    {
        $skin_records->transform(function (SkinRecord $skin_record) {
            if (($skin_record->waxSkinRecords)->count() > 0) {

                $skin_record->skin_data = SkinRecordpresenter::assembleWaxSkinRecord($skin_record->waxSkinRecords);

            } else if (($skin_record->camSkinRecords)->count() > 0) {

                $skin_record->skin_data = SkinRecordpresenter::assembleCamSkinRecord($skin_record->camSkinRecords);

            } else {

                $skin_record->skin_data = SkinRecordpresenter::assembleHmSkinRecord($skin_record->hmSkinRecords);

            }

            $skin_record->store_name = $skin_record->store->name;
            $skin_record->store_img = $skin_record->store->store_img;

            $skin_record->addhidden(['store', 'waxSkinRecords', 'camSkinRecords', 'hmSkinRecords']);

            return $skin_record;
        });

        return $skin_records;
    }

    /**
     * @param $customer_id
     * @return mixed
     */
    private function getStoreIdsByCustId($customer_id): array
    {
        return Store::where('customer_id', $customer_id)->pluck('id')->all();
    }

}
